﻿using StarPizza.Model.User;
using StarPizza.ViewModel.User;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace StarPizza.ViewModel.ClientSystem
{
    class ClientRegistrator : AbstractClientData
    {
        public ClientRegistrator(UserSpace userSpace)
            :base(userSpace)
        {
            userSpace.AuthenticationStateChanged += userSpace_AuthenticationStateChanged;
        }

        void userSpace_AuthenticationStateChanged(AuthenticationState newState)
        {
            switch (newState)
            {
                case AuthenticationState.Off:
                case AuthenticationState.On:
                    IsVisible = false;
                    break;
                case AuthenticationState.Registrating:
                    IsVisible = true;
                    break;
            }
        }

        public void Registrate()
        {
            try
            {
                string encryptedPassword = UserAuthenticator.EncryptPassword(Password);
                userSpace.RegistrateClient(Login, encryptedPassword, Name, District, Address);
                userSpace.Authenticate(Login, encryptedPassword);
            }
            catch (Exception e)
            {
                 MessageBox.Show(
                    messageBoxText: e.Message,
                    caption: "Registration error",
                    button: MessageBoxButton.OK,
                    icon: MessageBoxImage.Error
                    );
            }
            Password = "";
            ConfirmedPassword = "";
        }
        public void CancelRegistration()
        {
            userSpace.CancelRegistration();
        }
    }
}
